﻿//© 2004 - 2008 ActiveModules, Inc. All Rights Reserved
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace DotNetNuke.Modules.ActiveForums
{
#region Templates
	public class Templates
	{
		public enum TemplateTypes: int
		{
			All, //0
			System, //1
			ForumView, //2
			TopicView, //3
			TopicsView, //4
			TopicForm, //5
			ReplyForm, //6
			QuickReplyForm, //7
			Email, //8
			Profile, //9
			ModEmail, //10
			PostInfo, //11
			MailConnector //12
		}

	}
#endregion
#region TemplateInfo
	public class TemplateInfo
	{
#region Private Members
		private int _TemplateId;
		private int _PortalId;
		private int _ModuleId;
		private Templates.TemplateTypes _TemplateType;
		private bool _IsSystem;
		private string _Title;
		private string _Subject;
		private string _Template;
		private string _TemplateHTML;
		private string _TemplateText;
		private DateTime _DateCreated;
		private DateTime _DateUpdated;
#endregion
#region Public Properties
		public int TemplateId
		{
			get
			{
				return _TemplateId;
			}
			set
			{
				_TemplateId = value;
			}
		}
		public int PortalId
		{
			get
			{
				return _PortalId;
			}
			set
			{
				_PortalId = value;
			}
		}
		public int ModuleId
		{
			get
			{
				return _ModuleId;
			}
			set
			{
				_ModuleId = value;
			}
		}
		public Templates.TemplateTypes TemplateType
		{
			get
			{
				return _TemplateType;
			}
			set
			{
				_TemplateType = value;
			}
		}
		public bool IsSystem
		{
			get
			{
				return _IsSystem;
			}
			set
			{
				_IsSystem = value;
			}
		}
		public string Subject
		{
			get
			{
				return _Subject;
			}
			set
			{
				_Subject = value;
			}
		}
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title = value;
			}
		}
		public string Template
		{
			get
			{
				return _Template;
			}
			set
			{
				_Template = value;
			}
		}
		public string TemplateHTML
		{
			get
			{
				return _TemplateHTML;
			}
			set
			{
				_TemplateHTML = value;
			}
		}
		public string TemplateText
		{
			get
			{
				return _TemplateText;
			}
			set
			{
				_TemplateText = value;
			}
		}
		public DateTime DateCreated
		{
			get
			{
				return _DateCreated;
			}
			set
			{
				_DateCreated = value;
			}
		}
		public DateTime DateUpdated
		{
			get
			{
				return _DateUpdated;
			}
			set
			{
				_DateUpdated = value;
			}
		}
#endregion

	}
#endregion
#region Template Controller
	public class TemplateController
	{
#region Public Methods
		//'<summary>
		//'Function to save template.</summary>
		//'<param name="info">TemplateInfo object</param>
		public int Template_Save(TemplateInfo info)
		{
            return Convert.ToInt32(DataProvider.Instance().Templates_Save(info.TemplateId, info.PortalId, info.ModuleId, (int)info.TemplateType, info.IsSystem, info.Title, info.Subject, info.Template));
		}
		public List<TemplateInfo> Template_List(int PortalId, int ModuleId)
		{
			return GetTemplateList(PortalId, ModuleId, Templates.TemplateTypes.All);
		}
		public List<TemplateInfo> Template_List(int PortalId, int ModuleId, Templates.TemplateTypes TemplateType)
		{
			return GetTemplateList(PortalId, ModuleId, TemplateType);
		}

		public void Template_Delete(int TemplateId, int PortalId, int ModuleId)
		{
			DataProvider.Instance().Templates_Delete(TemplateId, PortalId, ModuleId);
		}
		public TemplateInfo Template_Get(string TemplateName, int PortalId, int ModuleId)
		{
			TemplateInfo ti = null;
			foreach (TemplateInfo tiWithinLoop in Template_List(PortalId, ModuleId))
			{
				ti = tiWithinLoop;
				if (TemplateName.ToUpperInvariant() == tiWithinLoop.Title.ToUpperInvariant())
				{
					tiWithinLoop.TemplateHTML = GetHTML(tiWithinLoop.Template);
					tiWithinLoop.TemplateText = GetText(tiWithinLoop.Template);
					return tiWithinLoop;
					break;
				}
			}
			return ti;
		}
		public TemplateInfo Template_Get(int TemplateId)
		{
			return Template_Get(TemplateId, -1, -1);
		}
		public TemplateInfo Template_Get(int TemplateId, int PortalId, int ModuleId)
		{
			TemplateInfo info = new TemplateInfo();
			IDataReader dr = DataProvider.Instance().Templates_Get(TemplateId, PortalId, ModuleId);
			while (dr.Read())
			{
				try
				{
					info.TemplateId = Convert.ToInt32(dr["TemplateId"]);
					info.PortalId = Convert.ToInt32(dr["PortalId"]);
					info.ModuleId = Convert.ToInt32(dr["ModuleId"]);
					info.TemplateType = (Templates.TemplateTypes)(Convert.ToInt32(dr["TemplateType"]));
					info.IsSystem = Convert.ToBoolean(dr["IsSystem"]);
					info.Title = Convert.ToString(dr["Title"]);
					info.Subject = Convert.ToString(dr["Subject"]);
					info.Template = Convert.ToString(dr["Template"]);
					info.TemplateHTML = GetHTML(Convert.ToString(dr["Template"]));
					info.TemplateText = GetText(Convert.ToString(dr["Template"]));
					if (dr.IsDBNull(8))
					{
						info.DateCreated = Utilities.NullDate();
					}
					else
					{
						info.DateCreated = Convert.ToDateTime(dr["DateCreated"]);
					}
					if (dr.IsDBNull(9))
					{
						info.DateUpdated = Utilities.NullDate();
					}
					else
					{
						info.DateUpdated = Convert.ToDateTime(dr["DateUpdated"]);
					}

				}
				catch (Exception ex)
				{
					return null;
				}


			}
			return info;
		}
#endregion
#region Private Methods
		private string GetHTML(string Template)
		{
			try
			{
				if (Template.Contains("<html>"))
				{
					string sHTML = string.Empty;
					System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
					xDoc.LoadXml(Template);
					System.Xml.XmlNode xNode = null;
					System.Xml.XmlNode xRoot = xDoc.DocumentElement;
					xNode = xRoot.SelectSingleNode("/template/html");
					sHTML = xNode.InnerText;
					return sHTML;
				}
				else
				{
					return Template;
				}
			}
			catch (Exception ex)
			{
				return Template;
			}

		}
		private string GetText(string Template)
		{
			try
			{
				if (Template.Contains("<plaintext>"))
				{
					string sText = string.Empty;
					System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
					xDoc.LoadXml(Template);
					System.Xml.XmlNode xNode = null;
					System.Xml.XmlNode xRoot = xDoc.DocumentElement;
					xNode = xRoot.SelectSingleNode("/template/plaintext");
					sText = xNode.InnerText;
					return sText;
				}
				else
				{
					return Template;
				}

			}
			catch (Exception ex)
			{
				return Template;
			}

		}
		private List<TemplateInfo> GetTemplateList(int PortalId, int ModuleId, Templates.TemplateTypes TemplateType)
		{
			try
			{
				List<TemplateInfo> tl = new List<TemplateInfo>();
				IDataReader dr = null;
				if (TemplateType == Templates.TemplateTypes.All)
				{
					dr = DataProvider.Instance().Templates_List(PortalId, ModuleId, -1);
				}
				else
				{
					dr = DataProvider.Instance().Templates_List(PortalId, ModuleId, (int)TemplateType);
				}
				dr.Read();
				dr.NextResult();
				TemplateInfo ti = null;
				while (dr.Read())
				{
					ti = new TemplateInfo();
					if (dr.IsDBNull(8))
					{
						ti.DateCreated = Utilities.NullDate();
					}
					else
					{
						ti.DateCreated = Convert.ToDateTime(dr["DateCreated"]);
					}
					if (dr.IsDBNull(9))
					{
						ti.DateUpdated = Utilities.NullDate();
					}
					else
					{
						ti.DateUpdated = Convert.ToDateTime(dr["DateUpdated"]);
					}
					ti.IsSystem = Convert.ToBoolean(dr["IsSystem"]);
					ti.ModuleId = Convert.ToInt32(dr["ModuleID"]);
					ti.PortalId = Convert.ToInt32(dr["PortalId"]);
					ti.Subject = Convert.ToString(dr["Subject"]);
					ti.Template = Convert.ToString(dr["Template"]);
					ti.TemplateHTML = GetHTML(ti.Template);
					ti.TemplateId = Convert.ToInt32(dr["TemplateId"]);
					ti.TemplateText = GetText(ti.Template);
					ti.TemplateType = (Templates.TemplateTypes)(dr["TemplateType"]);
					ti.Title = Convert.ToString(dr["Title"]);
					tl.Add(ti);
				}
				dr.Close();
				return tl;
			}
			catch (Exception ex)
			{
				return null;
			}
		}
#endregion
	}
#endregion
}
